﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;

namespace Dragon
{
    public partial class wfVisitDaily : System.Web.UI.Page
    {
        DataService ds = new DataService();       

        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> roles = new List<string> { "All", "BAM" };
            if (Session["Username"] == null || roles.Contains(Session["UserType"].ToString()) == false)
            {
                Response.Redirect("~/wfLogin.aspx");
            }

            if (!IsPostBack)
            {                
                this.ReportViewer1.Visible = false;                
                this.txtFromDate.Text = "01/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("yyyy");
                this.txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");                
            }
        }

        protected void btnRunReport_Click(object sender, EventArgs e)
        {
            DateTime fromDate = new DateTime();
            DateTime toDate = new DateTime();

            try
            {
                fromDate = DateTime.ParseExact(this.txtFromDate.Text, "dd/MM/yyyy", null);
                toDate = DateTime.ParseExact(this.txtToDate.Text, "dd/MM/yyyy", null);
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "<script>alert('Ngày tháng không hợp lệ!');</script>");
                return;
            }

            string ProvinceCode = "";
            if (this.ddlProvince.SelectedIndex != 0)            
                ProvinceCode = this.ddlProvince.SelectedValue.ToString();            
            
            string BASCode = "";
            if (this.ddlBAS.SelectedIndex != 0)            
                BASCode = this.ddlBAS.SelectedValue.ToString();
            
            ExecReport(fromDate, toDate, ProvinceCode, BASCode);

            this.ReportViewer1.Visible = true;
            DataTable dt = ds.GetData("select * from ReportVisitDaily V inner join ReportIndex I on V.IndexCode = I.IndexCode");
            ReportDataSource rds = new ReportDataSource("dsVisitDaily", dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
        }

        private void ExecReport(DateTime fromDate, DateTime toDate, string provinceCode, string bASCode)
        {
            //Lay danh sach ma BAS cho bao cao
            string sql = "select DragonCode from BAS where ((@ProvinceCode != '' and Province = @ProvinceCode) or (@ProvinceCode = '' and 1 = 1)) and"
                            + "((@BASCode != '' and Code = @BASCode) or (@BASCode = '' and 1 = 1)) and DragonCode is not null";

            SqlParameter pProvinceCode = new SqlParameter("@ProvinceCode", provinceCode);
            SqlParameter pBASCode = new SqlParameter("@BASCode", bASCode);
            DataTable dt = ds.GetData(sql, pProvinceCode, pBASCode);

            //Xoa so lieu cu trong bang ReportVisitDaily
            sql = "delete from ReportVisitDaily";
            ds.SetData(sql);

            //Lan luot dua so lieu moi vao bang ReportVisitDaily
            ds.Connnection.Open();
            sql = "exec sp_Report_Visit_Daily @ReportDate, @UserCode";
            SqlCommand sqlCom = new SqlCommand(sql, ds.Connnection);
            SqlTransaction sqlTrans = ds.Connnection.BeginTransaction(IsolationLevel.ReadCommitted);
            sqlCom.Transaction = sqlTrans;

            int soNgay = (int)(toDate - fromDate).TotalDays + 1;
            int soBAS = dt.Rows.Count;
            for (int i = 0; i < soNgay; i++)
                for (int j = 0; j < soBAS; j++)
                    try
                    {
                        SqlParameter[] param = new SqlParameter[2];
                        param[0] = new SqlParameter("@ReportDate", fromDate.AddDays(i));
                        param[1] = new SqlParameter("@UserCode", dt.Rows[j][0].ToString());
                        sqlCom.Parameters.Clear();
                        sqlCom.Parameters.AddRange(param);
                        sqlCom.ExecuteNonQuery();
                    }
                    catch { }
            sqlTrans.Commit();
            ds.Connnection.Close();
        }
        
        protected void ProvinceOnDataBound(object sender, EventArgs e)
        {
            this.ddlProvince.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlProvince.SelectedIndex = 0;
        }
 
       protected void BASOnDataBound(object sender, EventArgs e)
        {
            this.ddlBAS.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlBAS.SelectedIndex = 0;
        }      
    }
}